package copyengine.resource.lazyload
{
	import copyengine.resource.CEResConfig;
	import copyengine.resource.CEResManger;
	import copyengine.resource.dto.LazyLoadInitDto;
	import copyengine.resource.dto.ResObjectRequestParamDTO;

	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class CELazyLoadSpriteContainer extends Sprite implements ICELazyLoadContainer
	{
		private var fileName:String;
		private var className:String;
		private var childMc:Sprite;
		private var loadComplateCallBackFunctionVector:Vector.<Function>;

		public function CELazyLoadSpriteContainer()
		{
		}

		public function initialize(_dto:LazyLoadInitDto):void
		{
			fileName = _dto.fileName;
			className = _dto.className;
			LazyLoadContainerInitHelper.addLazyLoadIcon(_dto, this);
		}

		public function notificationLazyLoaderTargetLoadComplate():void
		{
			LazyLoadContainerInitHelper.removeLazyLoadIcon(this);

			var dto:ResObjectRequestParamDTO = new ResObjectRequestParamDTO();
			dto.fileType = CEResConfig.FILE_TYPE_SWF;
			dto.displayObjectType = CEResConfig.SWF_TYPE_SPRITE;
			dto.className = className;

			childMc = CEResManger.instance.getResFile(fileName).getObject(dto) as Sprite;
			addChild(childMc);
		}

		public function addLoadComplateCallBackFunction(_f:Function):void
		{
			if (childMc == null)
			{
				if (loadComplateCallBackFunctionVector == null)
				{
					loadComplateCallBackFunctionVector = new Vector.<Function>();
				}
				loadComplateCallBackFunctionVector.push(_f);
			}
			else
			{
				_f();
			}
		}

		public function removeLoadComplateCallBack(_f:Function):void
		{
			if (loadComplateCallBackFunctionVector != null)
			{
				for (var i:int = 0; i < loadComplateCallBackFunctionVector.length; i++)
				{
					if (loadComplateCallBackFunctionVector[i] == _f)
					{
						loadComplateCallBackFunctionVector.splice(i, 1);
						break;
					}
				}
			}
		}


	}
}
